package com.situ.helloandroid;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// Alt+Enter
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtn1;
    private Button mBtn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtn1 = (Button)findViewById(R.id.btn_1);
        mBtn2 = (Button) findViewById(R.id.btn_2);
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);

        // 给按钮加点击事件
        /*mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Btn1 点击事件");
                Toast.makeText(MainActivity.this, "Btn1点击事件",Toast.LENGTH_LONG).show();
            }
        });

        mBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Btn2 点击事件");
                Toast.makeText(MainActivity.this, "Btn2点击事件",Toast.LENGTH_LONG).show();
            }
        });*/
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                System.out.println("Btn1 点击事件");
                Toast.makeText(MainActivity.this, "Btn1点击事件",Toast.LENGTH_LONG).show();

                AlertDialog dialog;
                dialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Dialog对话框")
                        .setMessage("是否确定退出？")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_LONG).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show();
                            }
                        })
                        .create();
                dialog.show();
            break;
            case R.id.btn_2:
                System.out.println("Btn2 点击事件");
                Toast.makeText(MainActivity.this, "Btn2点击事件",Toast.LENGTH_LONG).show();
                int sum = 0;
                for (int i = 1; i <= 100; i++) {
                    sum += i;
                }
                System.out.println(sum);

                Intent intent = new Intent(this, LifeActivity.class);
                startActivity(intent);
                break;
        }
    }
}
